package com.cn.ant.modules.school.service;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.service.BaseService;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.modules.school.dao.SchoolDao;
import com.cn.ant.modules.school.entity.School;
import com.cn.ant.modules.score.dao.LqSchoolDao;
import com.cn.ant.modules.score.service.LqSchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学校信息Service
 * 
 * @author AntDream
 * @version 2015-08-14
 */
@Service
@Transactional(readOnly = true)
public class SchoolService extends BaseService {

    @Autowired
    private SchoolDao schoolDao;
    @Autowired
    private LqSchoolDao lqSchoolDao;
    @Autowired
    private LqSchoolService lqSchoolService;

    public School get(String id) {
        return schoolDao.get(id);
    }

    public School getByCode(String schoolCode, String schoolCodeSn) {
        return schoolDao.getByCode(schoolCode, schoolCodeSn);
    }

    public School queryVolunteerSchool(Map<String, Object> params) {
        List<School> schoolList = schoolDao.queryVolunteerSchool(params);
        if (schoolList != null && schoolList.size() > 0) {
            return schoolList.get(0);
        }
        return new School();
    }

    public List<School> findList(School school) {
        return schoolDao.findList(school);
    }

    public Page<School> findPage(Page<School> page, School school) {
        school.setPage(page);
        page.setList(schoolDao.findList(school));
        return page;
    }

    public List<School> searchSchool(Map<String, String> params) {
        return schoolDao.searchSchool(params);
    }

    @Transactional(readOnly = false)
    public void save(School school) {
        if (school.getIsNewRecord()) {
            school.preInsert();
            schoolDao.insert(school);
        } else {
            school.preUpdate();
            schoolDao.update(school);
        }
    }

    @Transactional(readOnly = false)
    public void delete(School school) {
        schoolDao.delete(school);
    }

    /**
     * 根据学校编码，获取志愿组合所属学校信息
     *
     * @param schoolCode
     * @return
     */
    public School querySchoolForVolunteer(String schoolCode, String schoolCodeSn, String kemuType, String batchLevel, int lqYear) {
        Map<String, Object> params = new HashMap<>();
        params.put("year", lqYear);
        params.put("schoolCode", schoolCode);
        params.put("schoolCodeSn", schoolCodeSn);
        params.put("kemuType", kemuType);
        params.put("batchLevel", batchLevel);
        School school = queryVolunteerSchool(params);
        int forecastScore = getForecastScore(schoolCode, schoolCodeSn, kemuType, batchLevel, lqYear);
        /*
        if (lqSchoolList != null && lqSchoolList.size() > 0) {
            for (int i = 0; i < lqSchoolList.size(); i++) {
                totalMinScore += Integer.parseInt(lqSchoolList.get(i).getTwfMinScore());
            }
            forecastScore = totalMinScore / lqSchoolList.size();
        }*/
        if (StringUtils.isNotBlank(school.getProvinceName()) && StringUtils.isNotBlank(school.getCityName())) {
            if (school.getProvinceName().trim().equals(school.getCityName().trim())) {
                school.setCityName("");
            }
        }
        school.setRemarks(forecastScore + "");
        return school;
    }

    /**
     * 获取预测投档线
     *
     * @param schoolCode
     * @return
     */
    public int getForecastScore(String schoolCode, String schoolCodeSn, String kemuType, String batchLevel, int lqYear) {
        Map<String, Object> params = new HashMap<>();
        params.put("year", lqYear);
        params.put("schoolCode", schoolCode);
        params.put("kemuType", kemuType);
        params.put("batchLevel", batchLevel);
        int forecastScore = schoolDao.getForecastScore(params);
        //获取实际投档线
        Map<String, String> params2 = new HashMap<>();
        params2.put("schoolCode", schoolCode);
        params2.put("schoolCodeSn", schoolCodeSn);
        params2.put("klType", kemuType);
        params2.put("batchLevel", batchLevel);
        params2.put("lqYear", String.valueOf(lqYear));
        int tdx = lqSchoolService.getLastTdx(params2);
        if (tdx > 0) {
            int abs1 = forecastScore - tdx;
            if (abs1 >= 0 && abs1 <= 15) {
                forecastScore = tdx + 5;
            }
            if (abs1 > 15) {
                forecastScore = tdx + 10;
            }
            if (abs1 < 0 && abs1 >= -15) {
                forecastScore = tdx - 5;
            }
            if (abs1 < -15) {
                forecastScore = tdx - 10;
            }
        }
        return forecastScore;
    }

    /**
     * 查询学校关联专业的录取线
     *
     * @param year
     * @param schoolCode
     * @param klType
     * @param batchLevel
     * @return
     */
    public List<Map<String, String>> querySchoolMajor(int year, String schoolCode, String schoolCodeSn, String klType, String batchLevel) {
        Map<String, String> params = new HashMap<>();
        params.put("year", year + "");
        params.put("schoolCode", schoolCode);
        params.put("schoolCodeSn", schoolCodeSn);
        params.put("klType", klType);
        params.put("batchLevel", batchLevel);
        return schoolDao.querySchoolMajor(params);
    }

    public List<Map<String, String>> querySchoolRank(Map<String, Object> params) {

        return schoolDao.querySchoolRank(params);
    }

    public List<Map<String,String>> queryFavSchool(Map<String,String> params) {
        return schoolDao.queryFavSchool(params);
    }
}